﻿using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;

namespace AnlianCommon.API.Commands.BasicInfos
{
    public class BasicInfoCreateHander : IRequestHandler<BasicInfoCreateCommand, Result>
    {
        private readonly IBaseRepository<BasicInfo> _basicInfoRepository;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="basicInfoRepository"></param>
        /// <param name="mapper"></param>
        public BasicInfoCreateHander(IBaseRepository<BasicInfo> basicInfoRepository, IMapper mapper)
        {
            _basicInfoRepository = basicInfoRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 处理命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BasicInfoCreateCommand request, CancellationToken cancellationToken)
        {

            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                BasicInfo basicinfo =_mapper.Map<BasicInfo>(request);
                var list = await _basicInfoRepository.Add(basicinfo);
                if (list > 0)
                {
                    basicinfo.CreatedOwnerId = "张三";
                    basicinfo.CreatedTime = DateTime.Now;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
